// 股票数据API服务

// 获取股票列表数据
export const getStockList = async (list) => {
  try {
    // 这里使用新浪股票API获取A股数据
    // 由于跨域限制，实际项目中建议使用后端代理或付费API
    const response = await uni.request({
        // url: 'https://hq.sinajs.cn/list=sh000001,sz399001,sz399006',
        url: `https://hq.sinajs.cn/list=${list.join(',')}`,
        method: 'GET'
    });
    
    if (response.statusCode === 200 && response.data) {
      // 解析股票数据
      const stockList = parseStockData(response.data);
      return stockList;
    }
    return [];
  } catch (error) {
    console.error('获取股票数据失败:', error);
    return [];
  }
};

// 解析股票数据
const parseStockData = (data) => {
  // 示例数据解析，实际项目中需要根据API返回格式调整
  return data.split('\n')
    .filter(line => line.trim())
    .map(line => {
      const [name, values] = line.split('="');
      const code = name.split('_')[2];
      const values_arr = values.split(',');
      const stockName = values_arr[0];
      const todayOpen = values_arr[1];
      const yesterdayClose = values_arr[2];
      const currentPrice = values_arr[3];
      const highPrice = values_arr[4];
      const lowPrice = values_arr[5];
      const dealCount = values_arr[8];
      const dealSum = values_arr[9];
      
      // 买卖盘数据
      const buy1_v = values_arr[10];
      const buy1_p = values_arr[11];
      const buy2_v = values_arr[12];
      const buy2_p = values_arr[13];
      const buy3_v = values_arr[14];
      const buy3_p = values_arr[15];
      const buy4_v = values_arr[16];
      const buy4_p = values_arr[17];
      const buy5_v = values_arr[18];
      const buy5_p = values_arr[19];
      
      const sell1_v = values_arr[20];
      const sell1_p = values_arr[21];
      const sell2_v = values_arr[22];
      const sell2_p = values_arr[23];
      const sell3_v = values_arr[24];
      const sell3_p = values_arr[25];
      const sell4_v = values_arr[26];
      const sell4_p = values_arr[27];
      const sell5_v = values_arr[28];
      const sell5_p = values_arr[29];
      
      return {
        name: stockName,
        code: code,
        newPrice: parseFloat(currentPrice),
        priceGap: parseFloat(currentPrice-yesterdayClose),
        fluctuate: ((currentPrice - yesterdayClose) / yesterdayClose * 100).toFixed(2),
        dealCount: parseFloat(dealCount),
        dealSum: parseFloat(dealSum),
        todayOpen: parseFloat(todayOpen),
        highPrice: (parseFloat(highPrice)).toFixed(2)*1,
        lowPrice: parseFloat(lowPrice),
        speed: ((currentPrice - yesterdayClose) / 720 * 100).toFixed(2), // 涨速需要根据分时数据计算
        // 买卖盘数据
        buy1_v: parseFloat(buy1_v),
        buy1_p: parseFloat(buy1_p),
        buy2_v: parseFloat(buy2_v),
        buy2_p: parseFloat(buy2_p),
        buy3_v: parseFloat(buy3_v),
        buy3_p: parseFloat(buy3_p),
        buy4_v: parseFloat(buy4_v),
        buy4_p: parseFloat(buy4_p),
        buy5_v: parseFloat(buy5_v),
        buy5_p: parseFloat(buy5_p),
        sell1_v: parseFloat(sell1_v),
        sell1_p: parseFloat(sell1_p),
        sell2_v: parseFloat(sell2_v),
        sell2_p: parseFloat(sell2_p),
        sell3_v: parseFloat(sell3_v),
        sell3_p: parseFloat(sell3_p),
        sell4_v: parseFloat(sell4_v),
        sell4_p: parseFloat(sell4_p),
        sell5_v: parseFloat(sell5_v),
        sell5_p: parseFloat(sell5_p)
      };
    });
};

// 搜索股票
export const searchStock = (keyword, stockList) => {
  if (!keyword) return stockList;
  
  const lowerKeyword = keyword.toLowerCase();
  return stockList.filter(stock => 
    stock.name.toLowerCase().includes(lowerKeyword) || 
    stock.code.toLowerCase().includes(lowerKeyword)
  );
};


// 热门概念
export const listClass = async () => {
    try {
      // 这里使用新浪股票API获取A股数据
      // 由于跨域限制，实际项目中建议使用后端代理或付费API
      const response = await uni.request({
        url: 'https://money.finance.sina.com.cn/q/view/newFLJK.php?param=class',
        method: 'GET'
      });
      
      if (response.statusCode === 200 && response.data) {
        // 解析股票数据
        let obj = JSON.parse(response.data.split('= ')[1])
        let keys = Object.keys(obj).slice(0,3)
        let stock = keys.map(key=>obj[key])
        
        const stockList = parseClassData(stock);
        return stockList;
      }
      return [];
    } catch (error) {
      console.error('获取股票数据失败:', error);
      return [];
    }
};

// 热门地区
export const listArea = async () => {
    try {
      // 这里使用新浪股票API获取A股数据
      // 由于跨域限制，实际项目中建议使用后端代理或付费API
      const response = await uni.request({
        url: 'https://money.finance.sina.com.cn/q/view/newFLJK.php?param=area',
        method: 'GET'
      });
      
      if (response.statusCode === 200 && response.data) {
        // 解析股票数据
        let obj = JSON.parse(response.data.split('= ')[1])
        let keys = Object.keys(obj).slice(0,3)
        let stock = keys.map(key=>obj[key])
        
        const stockList = parseClassData(stock);
        return stockList;
      }
      return [];
    } catch (error) {
      console.error('获取股票数据失败:', error);
      return [];
    }
};

// 热门行业
export const listIndustry = async () => {
    try {
      // 这里使用新浪股票API获取A股数据
      // 由于跨域限制，实际项目中建议使用后端代理或付费API
      const response = await uni.request({
        url: 'https://money.finance.sina.com.cn/q/view/newFLJK.php?param=industry',
        method: 'GET'
      });
      
      if (response.statusCode === 200 && response.data) {
        // 解析股票数据
        let obj = JSON.parse(response.data.split('= ')[1])
        let keys = Object.keys(obj).slice(0,3)
        let stock = keys.map(key=>obj[key])
        
        const stockList = parseClassData(stock);
        return stockList;
      }
      return [];
    } catch (error) {
      console.error('获取股票数据失败:', error);
      return [];
    }
};


const parseClassData = (data) => {
    // 示例数据解析，实际项目中需要根据API返回格式调整
    
    return data
      .filter(line => line.trim())
      .map(line => {
        
        const [tcode, tname,null1,null2,null3,trate,null4,null5,code,srate,null7,null8,sname] = line.split(',');
        console.log(code,tname,trate,sname,srate);
        
        return {code,tname,trate,sname,srate};
      });
  };